/**
 * Created by SJay on 2017/9/30.
 */
import TranslateRegularName from './TranslateRegularName'

let getLastStockCountNum = (stockThreshold, stockCount, weightUtils, minSaleQty) => {
  var lastStockCountNum = stockCount - minSaleQty - stockThreshold
  if (lastStockCountNum < 0 || weightUtils == 0) {
    return 0
  }
  lastStockCountNum = Math.floor(lastStockCountNum / weightUtils) + 1
  return lastStockCountNum
}


let translateDetailData = (data, nn) => {
  var mallProdVo = data.mallProdVo // 营销中心信息
  var price = 0
  var spacialPrice = 0
  var normalprice = 0
  var regularLabel = []
  if (mallProdVo) {
    price = mallProdVo.price || 0
    spacialPrice = mallProdVo.killPrice || mallProdVo.spacialPrice || mallProdVo.specPrice || 0
    if (spacialPrice) {
      var normalprice = price//商品原价
    }
    regularLabel = mallProdVo.promotionalVoList.map(item => {
      return TranslateRegularName(item)
    })
  }

  var barcode = data.barcode //条形码
  var getConfigParams = window.getConfigParams || {} //商城配置信息
  var goodstatus = data.goodstatus || data.goodStatus //4 下架
  var stockThreshold = getConfigParams.stockThreshold || 0 //商品不足的临界值
  var goodsid = data.goodsid || data.goodsId || '' //商品id
  var goodscode = data.goodscode || data.goodsCode || '' //商品编码
  var goodsname = data.goodsname || data.goodsName || '' //商品名称
  var wsprice = spacialPrice || data.wsprice || data.wsPrice || 0 // 销售价格
  var inActivity = data.inActivity || data.inActivity || 0 //是否在进行活动
  var showWsPrice = wsprice // 展示价格
  var showNormalPrice = normalprice // 展示原价
  var goodsunit = data.goodsunit || data.goodsUnit || '' //商品单位
  var prodarea = data.prodarea || data.prodArea || '暂无' //出产地
  var enterprisename = data.enterprisename || data.enterpriseName || '' //企业名称
  var urllist = data.urllist || data.urlList || [] // 图片列表
  var showUnit = data.mallUnit //展示商品单位
  var preorder = data.preorder || false  //选中状态
  var subtitle = data.subtitle || data.subTitle || '' //副标题
  var stockcount = data.stockcount || data.stockCount || '' //商品库存
  var distributionprocessingtime = data.distributionprocessingtime
  var showstockcount = stockcount // 商品展示库存
  var goodstype = data.goodstype || data.goodsType || '' //商品类型
  var goodsmodel = data.goodsmodel || data.goodsModel || '' //商品模块
  var goodsspec = data.goodsspec || data.goodsSpec || '' //商品规格
  var featurelabel = parseInt(data.memo4) || 0  //商品特色标签列表
  var rid = data.rid || '' //购物车id
  var distributiontype = data.distributiontype || data.distributionType || 0 //0常规1 大件
  var shopId = parseInt(data.storeid) || localStorage.getItem('shopId') || '' // 店铺Id
  var voidsum = data.voidsum || data.voidSum  //称重价格
  var goodsNum = data.goodsNum || 0 // 购买数量
  var ladderqty = data.ladderqty || data.ladderQty || 1  //阶梯重量
  var minsaleqty = data.minsaleqty || data.minsaleQty || 1 // 最小重量
  var weighingsmall = data.weighingsmall || data.weighingSmall || 0 //按只称重最小量
  var weighingbig = data.weighingbig || data.weighingBig || 1 // 按只称重最大单位
  var processList = data.processlist || data.processList //
  var labellist = data.labellist ? Object.prototype.toString.call(data.labellist) == '[object Array]' ? data.labellist : JSON.parse(data.labellist) : [] //特色标签列表
  var mallunit = data.mallunit || data.mallUnit || '' // 称重按只 单位
  var lastStockCountNum  //剩余库存
  var labellistFir = labellist.find((item) => {
    return item.labelseq == 1
  })
  var labellistThir = labellist.find((item) => {
    return item.labelseq == 3
  })
  var labellistFor = labellist.find((item) => {
    return item.labelseq == 4
  })
  var labellistSec = labellist.find((item) => {
    return item.labelseq == 2
  })
  var mainpho = data.mainPic
  var mainPic
  var eachPrice = wsprice * weighingbig
  labellistFir = labellistFir ? labellistFir.labelcontent : ''
  labellistSec = labellistSec ? labellistSec.labelcontent : ''
  labellistThir = labellistThir ? labellistThir.labelcontent : ''
  labellistFor = labellistFor ? labellistFor.labelcontent : ''

  if (goodstype == 7 || goodstype == 6) {
    goodsunit = goodsunit.toLowerCase()
    showUnit = '500g'
    goodsunit == 'kg'
    showWsPrice = 0.5 * wsprice
    showNormalPrice = 0.5 * normalprice
    // }
  }
  if (goodstype == 6) {
    ladderqty = weighingbig
    minsaleqty = weighingbig
    showstockcount = Math.floor(stockcount / weighingbig)
    goodsunit = mallunit
    // showUnit = mallunit
    goodsspec = '约' + weighingsmall + '-' + weighingbig + 'kg/' + goodsunit
  }

  if (goodstype == 7) {
    showstockcount = parseFloat(stockcount).toFixed(2)
    lastStockCountNum = showstockcount
  }
  showWsPrice = parseFloat(showWsPrice).toFixed(2)
  showNormalPrice = parseFloat(showNormalPrice).toFixed(2)
  lastStockCountNum = getLastStockCountNum(stockThreshold, stockcount, ladderqty, minsaleqty)
  var newData = {
    barCode: barcode,
    city: prodarea,
    distributionprocessingtime,//准备时间
    eachPrice,
    goodsCode: goodscode,
    goodsId: goodsid,
    goodsName: goodsname,
    goodsPrice: wsprice ? parseFloat(wsprice) : 0,
    goodsType: goodstype ? parseInt(goodstype) : 0,
    goodsModel: goodsmodel,
    goodsSpec: goodsspec || '',
    goodsStatus: goodstatus,
    goodsUnit: goodsunit,
    goodsDeliveryType: distributiontype,
    goodsNum,
    inActivity, //是否有活动
    isStockCountOver: lastStockCountNum < goodsNum, //购买数量是否超过库存
    featureLabel: featurelabel ? featurelabel : '',
    processList: processList,
    preorder: preorder,
    lastStockCountNum,  //剩余可购买数量
    labellistFir,
    labellistSec,
    labellistThir,
    labellistFor,
    minUtils: 1,
    mainPic: null,
    minSaleQty: minsaleqty,
    normalPrice: normalprice,
    regularTypeLabel: data.regularTypeLabel || '特价',
    rid: rid,
    showWsPrice,
    showNormalPrice,
    showUnit,
    stockCount: stockcount ? parseFloat(stockcount) : 0,
    showStockCount: showstockcount,
    shopName: enterprisename,
    shoreCode: shopId,
    subTitle: subtitle,
    voidsum: data.voidsum == 0 ? '' + data.voidsum : '', //称重价格
    weightType: 7,
    weightUtils: ladderqty,
    weighingSmall: weighingsmall,
    weighingBig: weighingbig,
    regularLabel
  }
  if (!urllist) {
    mainPic = ''
  }
  else if (urllist.length == 0) {
    mainPic = ''
  } else {
    mainPic = urllist.find((item) => {
      return parseInt(item.Imgurltype) == 1 || parseInt(item.imgurltype) == 1
    }) || urllist.find((item) => {
      return parseInt(item.Imgurltype) == 0 || parseInt(item.imgurltype) == 0
    })
    if (mainPic) {
      mainPic = mainPic.imgurl.split(' ')[0]
    }
  }

  newData.mainPic = mainpho || mainPic || 'static/image/demo2.png';
  return newData
}
export {translateDetailData, getLastStockCountNum}
